﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Messaging;
using HandyControl.Controls;
using Infov1.Commands;
using Infov1.Models;
using Infov1.ViewModels;
using Infov1.ViewModels.WorkRecord;
using SqlSugar;
namespace Infov1.Views.WorkRecord
{
    /// <summary>
    /// CustomerSelectDialog.xaml 的交互逻辑
    /// </summary>
    public partial class CustomerSelectDialog : System.Windows.Window
    {
        public CustomerSelectDialog()
        {
            InitializeComponent();
            this.DataContext = new CustomerSelectViewModel();
        }
        //加载后设置焦点在datagrid
        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            if (dataGrid.Items.Count > 0)
            {
                dataGrid.SelectedIndex = 0;
                dataGrid.Focus();
            }
        }
        //禁用datagrid 默认回车跳转下一列，设置焦点始终在
        private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    e.Handled = true;
                    OnConfirmClick(null, null);
                    break;
                case Key.Up:
                case Key.Down:
                    e.Handled = true;
                    int newIndex = dataGrid.SelectedIndex;
                    if (e.Key == Key.Up && newIndex > 0)
                    {
                        newIndex--;
                    }
                    else if (e.Key == Key.Down && newIndex < dataGrid.Items.Count - 1)
                    {
                        newIndex++;
                    }
                    // 更新选中项
                    dataGrid.SelectedIndex = newIndex;
                    // 确保选中项可见
                    dataGrid.ScrollIntoView(dataGrid.SelectedItem);
                    // 保持焦点
                    dataGrid.Focus();
                    break;
            }
        }
        //键盘事件
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                OnConfirmClick(null, null);
            }
            else if (e.Key == Key.Escape)
            {
                OnCancelClick(null, null);
            }
        }
        //监听窗口状态
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            if (DataContext is CustomerSelectViewModel vm)
            {
                // 监听ViewModel的DialogResult属性变化
                vm.PropertyChanged += (s, args) =>
                {
                    if (args.PropertyName == nameof(CustomerSelectViewModel.DialogResult))
                    {
                        // 当DialogResult改变时，关闭窗口
                        if (vm.DialogResult.HasValue)
                        {
                            this.DialogResult = vm.DialogResult.Value;
                            this.Close();
                        }
                    }
                };

                // 设置DataGrid的焦点
                if (this.FindName("customerGrid") is DataGrid grid)
                {
                    grid.Focus();
                }
            }
        }
        //确定按钮
        private void OnConfirmClick(object sender, RoutedEventArgs e)
        {
            var vm = DataContext as CustomerSelectViewModel;
            if (vm?.SelectedCustomer != null)
            {
                DialogResult = true;
                Close();
            }
            else
            {
                HandyControl.Controls.Growl.Warning("请选择一个客户！");
            }
        }
        //取消按钮
        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
        //双击确定
        private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            OnConfirmClick(null, null);
        }
   



    }

}
